package com.rtmart.image.controller;

import com.rtmart.image.utils.FileTools;
import com.rtmart.image.utils.ResponseBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class OneImage {
    @Value("${image.location}")
    private String indexImagePath;

    @PostMapping("/admin/oneImage/{type}")
    public ResponseBuilder postOneImage(@PathVariable String type, @RequestParam("file") MultipartFile file){
        //建立目录
        File filePath = new File(indexImagePath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        if(FileTools.savaFile(file, new File(indexImagePath + type + ".jpg"))){
            return ResponseBuilder.build(ResponseBuilder.Code.SUCCESS);
        } else {
            return ResponseBuilder.build(ResponseBuilder.Code.ERROR);
        }
    }
}
